#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <set>

using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::map;
using std::set;
using std::ifstream;
using std::istringstream;
using std::cerr;

class TextQuery
{
public:
    void readFile(const string & filename){
        ifstream ifs(filename);
        if(!ifs.good()){
            cerr<<filename<<" is not found"<<endl;
            return;
        }

        string line;
        string word;
        int line_sub = 1;
        while(getline(ifs,line)){
            //记录每一行
            _lines.emplace_back(line);
            istringstream iss(line);   
            while(iss>>word){
                if(!is_digit(word)){
                    //记录单词以及出现次数
                    ++ _dict[word];
                    //记录单词以及出现行数
                    _wordNumbers[word].insert(line_sub);
                }
            }
                line_sub++;
        }
    }
    bool is_digit(const string &word){
        for(auto &it : word){
            if(isdigit(it)){
                return true;
            }
        }   
        return false;
    }

    void query(const string & word){
            cout<<word<<" occurs "<<_dict[word]<<" times"<<endl;
            for(auto &it:_wordNumbers[word]){
                cout<<"(lien "<< it<<") "<<_lines[it-1]<<endl;
        }
    }//

private:
    vector<string> _lines;
    map<string, set<int> > _wordNumbers;
    map<string, int> _dict;
};

//程序测试用例
int main(int argc, char *argv[])
{

    TextQuery tq;
    tq.readFile("china_daily.txt");
    tq.query("with");
    return 0;
}

